package com.newtouch.bxzs.business.statistics.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.business.personal.mapper.PersonalMapper;
import com.newtouch.bxzs.business.statistics.mapper.MemberIssueSumMapper;
import com.newtouch.bxzs.business.statistics.mapper.OrganizationIssueSumMapper;
import com.newtouch.bxzs.business.statistics.mapper.OrganizationMapper;
import com.newtouch.bxzs.business.statistics.model.bo.MemberIssueSumBO;
import com.newtouch.bxzs.business.statistics.model.bo.OrganizationIssueSumBO;
import com.newtouch.bxzs.business.statistics.model.vo.MemberIssueSumVO;
import com.newtouch.bxzs.business.statistics.model.vo.OrganizationIssueSumVO;
import com.newtouch.bxzs.business.statistics.service.MemberIssueSumService;
import com.newtouch.bxzs.common.base.model.User;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 机构出单汇总查询
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/21 13:24</td>
 * <td>lingshouyi</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author lingshouyi
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class MemberIssueSumImpl implements MemberIssueSumService {

    @Autowired
    private MemberIssueSumMapper memberIssueSumMapper;

    @Autowired
    private OrganizationIssueSumMapper organizationIssueSumMapper;

    @Autowired
    private PersonalMapper personalMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    @Override
    public IPage<MemberIssueSumBO> getMemberIssueList(MemberIssueSumVO memberIssueSumVO) {
        if (BeanUtil.isEmpty(memberIssueSumVO)) {
            return null;
        }
        /* 根据机构编码获取该机构业务员 */
        List<String> userIdList = new ArrayList<>();
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("USER_ORGANIZATION_CODE", memberIssueSumVO.getOrganizationCode());
        List<User> userList = personalMapper.selectList(qw);
        if (CollectionUtils.isEmpty(userList)) {
            return null;
        }
        /* 获取每个员工id */
        for (User user : userList) {
            userIdList.add(user.getUserId());
        }
        Page<MemberIssueSumBO> page = new Page<>(memberIssueSumVO.getCurrentPage(), memberIssueSumVO.getPageSize());
        IPage<MemberIssueSumBO> memberIssueList = memberIssueSumMapper.getMemberIssueList(page, userIdList,
                DateUtil.beginOfDay(memberIssueSumVO.getStartTime()),DateUtil.endOfDay(memberIssueSumVO.getEndTime()));
        /*排名*/
        Integer n = 1;
        for (MemberIssueSumBO memberIssueSumBO : memberIssueList.getRecords()) {
            memberIssueSumBO.setRank(n);
            n++;
        }
        return memberIssueList;
    }

    @Override
    public IPage<OrganizationIssueSumBO> getOrganizationIssueSumList(OrganizationIssueSumVO organizationIssueSumVO) {
        if (BeanUtil.isEmpty(organizationIssueSumVO)) {
            return null;
        }
        if (CollectionUtils.isEmpty(organizationIssueSumVO.getOrganizationCodeList())) {
            String childOrganizationCodeStr = organizationMapper.selectChildOrganizationCodeStr(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserOrganizationCode());
            String[] childOrgCodeArr = childOrganizationCodeStr.split(",");
            organizationIssueSumVO.setOrganizationCodeList(Arrays.asList(childOrgCodeArr));
        }
        /*分页*/
        Page<OrganizationIssueSumBO> page = new Page<>(organizationIssueSumVO.getCurrentPage(), organizationIssueSumVO.getPageSize());
        /*判断传入机构编码是否为空*/
        if (CollectionUtils.isEmpty(organizationIssueSumVO.getOrganizationCodeList())) {
            List<String> organizationCodeList = new ArrayList<>();
            organizationCodeList.add(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserOrganizationCode());
            organizationIssueSumVO.setOrganizationCodeList(organizationCodeList);
        }
        return organizationIssueSumMapper.getOrganizationIssueSumList(page, organizationIssueSumVO.getOrganizationCodeList(),DateUtil.beginOfDay(organizationIssueSumVO.getStartTime()),DateUtil.endOfDay(organizationIssueSumVO.getEndTime()));
    }
}
